/******************************************************************/
/*                                                                */
/*  Winpooch : Windows Watchdog                                   */
/*  Copyright (C) 2004-2006  Benoit Blanchon                      */
/*                                                                */
/*  This program is free software; you can redistribute it        */
/*  and/or modify it under the terms of the GNU General Public    */
/*  License as published by the Free Software Foundation; either  */
/*  version 2 of the License, or (at your option) any later       */
/*  version.                                                      */
/*                                                                */
/*  This program is distributed in the hope that it will be       */
/*  useful, but WITHOUT ANY WARRANTY; without even the implied    */
/*  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR       */
/*  PURPOSE.  See the GNU General Public License for more         */
/*  details.                                                      */
/*                                                                */
/*  You should have received a copy of the GNU General Public     */
/*  License along with this program; if not, write to the Free    */
/*  Software Foundation, Inc.,                                    */
/*  675 Mass Ave, Cambridge, MA 02139, USA.                       */
/*                                                                */
/******************************************************************/


/******************************************************************/
/* Build configuration                                            */
/******************************************************************/

#define TRACE_LEVEL 2	// warning level


/******************************************************************/
/* Includes                                                       */
/******************************************************************/

// standard headers
#include <windows.h>
#include <io.h>
#include <tchar.h>
#include <stdio.h>
#include <shlwapi.h>

// project's headers
#include "BuildCount.h"
#include "ImgInfo.h"
#include "ProjectInfo.h"


/******************************************************************/
/* Internal macros                                                */
/******************************************************************/

#define arraysize(a) (sizeof(a)/sizeof((a)[0]))


/******************************************************************/
/* Internal constants                                             */
/******************************************************************/

static LPCTSTR g_aKernels[] = 
{ 
  TEXT("ntkrnlmp.exe"),
  TEXT("ntkrnlpa.exe"), 
  TEXT("ntkrpamp.exe"), 
  TEXT("ntoskrnl.exe") 
} ;


/******************************************************************/
/* Exported function                                              */
/******************************************************************/

BOOL IncompReport_Generate (LPCTSTR szFilename)
{
  FILE * fpOutput = _tfopen (szFilename, TEXT("wt"));

  int i ;

  _ftprintf (fpOutput,
	     TEXT("This file is an \"incompatibility report\" for %s version %s\n"),
	     TEXT(APPLICATION_NAME), TEXT(APPLICATION_VERSION_STRING)) ;

  _ftprintf (fpOutput,
	     TEXT("It contains information that allow Winpooch development team\n"
		  "to make Winpooch compatible with your system.\n"
		  "Please post in on the \"bug report system\" at the address :\n"
		  "http://sourceforge.net/tracker/?group_id=122629&atid=694093\n")) ;
	     
	   
  for( i=0 ; i<arraysize(g_aKernels) ; i++ )
    {
      TCHAR	szInputPath[MAX_PATH] ;
      VOID	*pBuffer ;
      UINT	nSize ;
      IMGINFO	info ;
      FILE	*fpInput ;
      DWORD	dwFoo, nVersionInfoSize ;

      _ftprintf (fpOutput, TEXT("----\n")) ;

      _ftprintf (fpOutput, TEXT("Image file name = %s\n"), g_aKernels[i]) ; 

      // Get C:\Windows\System32 location
      SHGetSpecialFolderPath (NULL, szInputPath, CSIDL_SYSTEM, FALSE) ;

      // append file name
      PathAppend (szInputPath, g_aKernels[i]) ;
      
      // open file for reading
      fpInput = _tfopen (szInputPath, TEXT("rb")) ;
      if( ! fpInput ) {
	_ftprintf (fpOutput, TEXT("--> NOT FOUND\n")) ; 
	continue ;
      }

      // get file size
      nSize = filelength(fileno(fpInput)) ;

      // alloc buffer for the whole file
      pBuffer = malloc (nSize) ;

      // read the whole file
      fread (pBuffer, nSize, 1, fpInput) ;

      fclose (fpInput) ;

      // get image info and print into output text file
      if( ImgInfo_GetInfo (&info, pBuffer, nSize) )
	{
	  _ftprintf (fpOutput, TEXT("Image time stamp = 0x%08X\n"), info.dwTimeStamp) ;  

	  _ftprintf (fpOutput, TEXT("Image check sum = 0x%08X\n"), info.dwCheckSum) ;  

	  _ftprintf (fpOutput, TEXT("Symbol file name = %hs\n"), info.szSymFilename) ;  

	  _ftprintf (fpOutput, TEXT("Symbol signature = %hs\n"), info.szSymSignature) ;	 
	}
      
      free (pBuffer) ;

      nVersionInfoSize = GetFileVersionInfoSize(szInputPath,&dwFoo) ;
      pBuffer = malloc (nVersionInfoSize) ;

      if( GetFileVersionInfo(szInputPath,0,nVersionInfoSize,pBuffer) ) 
	{
	  UINT			uLen ;
	  LPTSTR		szVersion ;
	  WORD			*pwTranslation ;

	  if( VerQueryValue (pBuffer, TEXT("\\VarFileInfo\\Translation"), 
			     (LPVOID *)&pwTranslation, &uLen) )
	    {
	      TCHAR szSubBlock[64] ;

	      _stprintf (szSubBlock,
			 TEXT("\\StringFileInfo\\%04x%04x\\FileVersion"),
			 pwTranslation[0], pwTranslation[1]) ;

	      if( VerQueryValue(pBuffer,szSubBlock, (void**)&szVersion,&uLen) ) 
		{
		  _ftprintf (fpOutput, 
			     TEXT("Version = %s\n"), szVersion) ;
		}
	    }
	}

      free (pBuffer) ;
    }

  fclose (fpOutput) ;

  return TRUE ;
}
